
package com.compuvest.web;

// <editor-fold defaultstate="collapsed" desc="import">
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
// </editor-fold>

public class SessionTrack implements Filter {

    private final static String VERSION        = "SessionTrack v1.1 2009-09-02";
    private final static String COOKIE_APPLIED = "_cookie_applied";

// <editor-fold defaultstate="collapsed" desc="init()">

    public void init(FilterConfig config) throws ServletException {
        System.out.println("Init: " + VERSION);
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="doFilter()">

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain nextFilter) throws IOException {

        try {
            if(request instanceof HttpServletRequest) {

                HttpSession session = ((HttpServletRequest)request).getSession();
                // set cookies
                if(session.getAttribute(COOKIE_APPLIED) == null) { // one time
                    session.setAttribute(COOKIE_APPLIED, Boolean.TRUE);

                    ServletContext context = session.getServletContext();

                    Basket basket = (Basket)session.getAttribute("basket");
                    if(basket != null) {
                        basket.getCompuvestCookies().setCompuvestCookies((HttpServletRequest)request, (HttpServletResponse)response);

                        AdvertShow advert = (AdvertShow)context.getAttribute("advertShow");
                        if(advert != null) {
                            // set click for advert
                            SQLBean sb = (SQLBean)context.getAttribute("sb");
                            if(sb != null) {
                                sb.saveCustomerLog(advert.getAdvertID(basket.getCompuvestCookies().getAdvertSite()));
                            }
                            else {
                                System.out.println("SessionTrack:doFilter:sb=null");
                            }
                        }
                        else {
                            System.out.println("SessionTrack:doFilter:advert=null");
                        }
                    }
                    else {
                        System.out.println("SessionTrack:doFilter:basket=null");
                    }

                }
            } // if

//            System.out.println("\t[" + name + "?" + query + "]");
//	    long startTime = System.currentTimeMillis(); hits++;

            nextFilter.doFilter(request, response);

//            System.out.println("\t[" + (System.currentTimeMillis() - startTime) + "] : h=" + hits);
            //	    long doTime = System.currentTimeMillis() - startTime;
/*
	    // find long select
	    if((doTime) > 400L) {
		String name = "";
		String query = "";
		if(request instanceof HttpServletRequest) {
		    name = ((HttpServletRequest)request).getRequestURI();
		    query = ((HttpServletRequest)request).getQueryString();
		}
		System.out.println("\t" + name + "?" + query + ": " + (doTime) + "ms");
	    }
*/
        }
        catch(ServletException se) { System.out.println("SessionTrack:doFilter()\n" + se); }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="destroy()">

    public void destroy() {
        System.out.println("Destroy: " + VERSION);
    }
// </editor-fold>

}
